package com.burger.counter.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.Item;
import com.burger.counter.beans.ShoppingCart;
import com.burger.counter.beans.SignatureBurgerBowl;
import com.burger.counter.beans.SignatureBurgerBun;
import com.burger.counter.beans.Starter;
import com.burger.counter.business.ItemBusiness;

/**
 * Servlet implementation class AddSigBurger
 */
public class AddSigBurger extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddSigBurger() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("i am in addSigBurger servlet");


ItemBusiness ItemBI = new ItemBusiness();
Item item=null;
ShoppingCart cart=null;
HttpSession session = request.getSession(true);

if(request.getParameter("submitBowl") !=null)
{
	System.out.println("Bowlllll");
	SignatureBurgerBowl signatureBurgerList[] = (SignatureBurgerBowl[]) session.getAttribute("signatureBurgerData");
	
	String[] values = request.getParameterValues("SigBurgerBowlData");	
	for(int i=0;i<values.length;i++)
		System.out.println("VAlue" + values[i]);
	if(signatureBurgerList != null && values != null)
	{
		for(int i=0;i<signatureBurgerList.length;i++)
		{
			  if(signatureBurgerList[i].getSigBurgerBowlId()==Integer.parseInt(values[0]))
			  {
				  int	pId = signatureBurgerList[i].getSigBurgerBowlId();
					System.out.println("b id is " + pId);
					
					String pname = signatureBurgerList[i].getSigBurgerBowlName();
					System.out.println("user nm is " + pname);
					
					
					
					String pdesc = signatureBurgerList[i].getSigBurgerBowlDesc();
					System.out.println("b desc is " + pdesc);
					
					Double	pPrice = signatureBurgerList[i].getSigBurgerBowlPrice();
					System.out.println("price is " + pPrice);
					int userId = (Integer)session.getAttribute("UserId");
					System.out.println("user id is " + userId);
					
					
				
					cart=(ShoppingCart)session.getAttribute("Cart");
					
					item = ItemBI.setItemDetails(pId,userId, pname, pPrice);
			  }
		}
		
	}
	
}
else if (request.getParameter("submitBun")!=null)
{
	System.out.println("Bunn");

	SignatureBurgerBun signatureBurgerBunList[] = (SignatureBurgerBun[]) session.getAttribute("signatureBurgerBunData");
	String[] values = request.getParameterValues("SigBurgerBunData");	
	for(int i=0;i<values.length;i++)
		System.out.println("VAlue" + values[i]);
	if(signatureBurgerBunList != null && values != null)
	{
		for(int i=0;i<signatureBurgerBunList.length;i++)
		{
			  if(signatureBurgerBunList[i].getSigBurgerBunId()==Integer.parseInt(values[0]))
			  {
				  int	pId = signatureBurgerBunList[i].getSigBurgerBunId();
					System.out.println("b id is " + pId);
					
					String pname = signatureBurgerBunList[i].getSigBurgerBunName();
					System.out.println("user nm is " + pname);
					
					
					
					String pdesc = signatureBurgerBunList[i].getSigBurgerBunDesc();
					System.out.println("b desc is " + pdesc);
					
					Double	pPrice = signatureBurgerBunList[i].getSigBurgerBunPrice();
					System.out.println("price is " + pPrice);
					int userId = (Integer)session.getAttribute("UserId");
					System.out.println("user id is " + userId);
					
					
				
					cart=(ShoppingCart)session.getAttribute("Cart");
					
					item = ItemBI.setItemDetails(pId,userId, pname, pPrice);
			  }
		}
		
	}
}


		
		
		
		
		if(cart == null){
			System.out.println("Creating new cart....................");
			cart = new ShoppingCart();
			cart.addItemInCart(item);
			session.setAttribute("Cart", cart);
		}else{
			System.out.println("Cart is already there..................");
			cart.addItemInCart(item);
			session.setAttribute("Cart", cart);
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/SignatureBurgerPage.jsp");
		dispatcher.forward(request, response);
	}
	}
	

